﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace VBAConvertor.Parsers
{
    public class EndWithParser : ILineParser
    {
        private static readonly Regex EndWithPattern =
            new Regex(
                @"^End With$",
                RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.Singleline);

        public string Parse(Stack<ParseStatus> statuses, string lineContent, LinesReader reader, out bool success, ref bool lineClosed)
        {
            var match = EndWithPattern.Match(lineContent);
            success = match.Success;
            if (!success) return lineContent;

            if (statuses.Peek().CurrentState != State.WithDef)
            {
                throw new Exception();
            }
            statuses.Pop();
            if (lineClosed)
            {
                throw new Exception();
            }
            lineClosed = true;
            return "";
        }
    }
}
